// pages/address2/address2.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name:"",
    phone:"",
    area:"",
    address:"",
    istop:false
  },
  changetop(){
    this.setData({
      istop:!this.data.istop
    })
  },
  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      area: e.detail.value
    })
  },
  saveaddress(){
    if(this.data.name && this.data.phone && this.data.area && this.data.address){
      let reg_tel = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
      if(!reg_tel.test(this.data.phone)){
        wx.showToast({
          title: '请输入正确手机号码',
          icon: 'none',
          duration: 2000
         })
         return;
      }
      let addresslist = wx.getStorageSync('addresslist');
    if(addresslist){
      if(this.data.istop){
        addresslist.unshift({
          name:this.data.name,
          phone:this.data.phone,
          area:this.data.area,
          address:this.data.address
        })
      }else{
        addresslist.push({
          name:this.data.name,
          phone:this.data.phone,
          area:this.data.area,
          address:this.data.address
        })
      }     
      wx.setStorageSync('addresslist', addresslist)
    }else{
      let lists = [{
        name:this.data.name,
        phone:this.data.phone,
        area:this.data.area,
        address:this.data.address
      }]
      wx.setStorageSync('addresslist', lists);
    }
    this.setData({
    name:"",
    phone:"",
    area:"",
    address:""
    })
    wx.navigateBack({
      delta: 1
    });
    }else{
      wx.showToast({
        title: '请输入完整信息',
        icon: 'none',
        duration: 2000
       })
    }
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})